<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr =  [
          { 'id': '29', 'pid': '',     'name': '总裁办' },
          { 'id': '2c', 'pid': '',     'name': '财务部' },
          { 'id': '2d', 'pid': '2c', 'name': '财务核算部'},
          { 'id': '2f', 'pid': '2c', 'name': '薪资管理部'},
          { 'id': 'd2', 'pid': '',     'name': '技术部'},
          { 'id': 'd3', 'pid': 'd2', 'name': 'Java研发部'},
          { 'id': '2g', 'pid': 'd2', 'name': 'Python研发部'},
        ]

        const newobj = {}  // 定义对象，映射对象中id，便捷寻找
        arr.forEach(item=>{
            if(!item.children){
                item.children = []
            }
            newobj[item.id] = item
        })
        console.log('原数组',arr);
        console.log('创建的新对象',newobj);

        newarr = [] // 存放属性结构的数据

        arr.forEach(item=>{
            const parent = newobj[item.pid]
            console.log(parent,'0000000000');
            if(parent){
                parent.children.push(item)
            }else{
                newarr.push(item)
            }
        })
        console.log(parent,'1111111111');
        console.log('newarr',newarr);
    </script>
</body>
</html>